package com.hj2023;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class firstautotest {
    //创建一个驱动对象来打开浏览器
    private ChromeDriver driver = new ChromeDriver();
    public void start_test() throws InterruptedException {
        driver.get("https://www.baidu.com");
    }
        //第一个简单的自动化示例
    public void dilireba_test() throws InterruptedException {

        //输入百度网址
        driver.get("https://www.baidu.com");
        Thread.sleep(3000);
        //找到百度输入框，并输入关键词“迪丽热巴”
        driver.findElement(By.cssSelector("#kw")).sendKeys("迪丽热巴");
        Thread.sleep(3000);
        //找到百度一下按钮，并点击

        driver.findElement(By.cssSelector("#su")).click();


    }

    /**
     * 定位元素的方法
     */
    public void dingwei_test() throws InterruptedException {
        //CSS选择器
//        String xwtext = driver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).getText();
//        System.out.println("xwtext:"+xwtext);
        //xpath选择器
//        driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("教资");
//        driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
//        Thread.sleep(3000);
//        driver.findElement(By.xpath("//*[@id=\"1\"]/div/div/h3/a"));
        String xwtxt = driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[2]/a/span[2]")).getText();
        System.out.println("期望的文本是：平凡岗位上的奋斗故事，实际打印出来的是："+xwtxt);
        //Thread.sleep(3000);
    }

    /**
     * 元素的操作
     */
    public void control_test() throws InterruptedException {
        //click
        //driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
        //driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(1) > a")).click();
        //submit提交
//        driver.findElement(By.cssSelector("#kw")).sendKeys("教资");
//        driver.findElement(By.cssSelector("#su")).submit();
//        driver.findElement(By.cssSelector("#kw")).sendKeys("火箭");
        //clear清除对象输入的文本内容
//        driver.findElement(By.cssSelector("#kw")).sendKeys("奇迹再现");
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#kw")).clear();
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#kw")).sendKeys("奥特曼");
//        Thread.sleep(3000);
        //获取文本 gettext
//        String bdtext = driver.findElement(By.cssSelector("#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title")).getText();
//        System.out.println("期望的文本:平凡岗位上的奋斗故事，实际的文本:"+bdtext);
        //百度一下是属性值，这里打印出来的文本为空
//        String bdtext = driver.findElement(By.cssSelector("#su")).getText();
//        System.out.println("期望的文本:百度一下，实际的文本:"+bdtext);
        //如何获取属性值 用getAttribute
        String bdtext1 = driver.findElement(By.cssSelector("#su")).getAttribute("type");
        String bdtext2 = driver.findElement(By.cssSelector("#su")).getAttribute("id");
        String bdtext3 = driver.findElement(By.cssSelector("#su")).getAttribute("value");
        String bdtext4 = driver.findElement(By.cssSelector("#su")).getAttribute("class");
        System.out.println("type:"+bdtext1+",id:"+bdtext2+",value:"+bdtext3+",class:"+bdtext4);
    }
    public void wait_test() throws InterruptedException {
//        driver.findElement(By.cssSelector("#kw")).sendKeys("教资");
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#su")).click();
//        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#\\33  > div > div:nth-child(1) > h3 > a"));
        //隐式等待 作用在 webdriver 的整个生命周期
//          driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
//          driver.findElement(By.cssSelector("#kw")).sendKeys("教资");
//          driver.findElement(By.cssSelector("#su")).click();
//          driver.findElement(By.cssSelector("#\\33  > div > div:nth-child(1) > h3 > a"));
          //显式等待
          driver.findElement(By.cssSelector("#kw")).sendKeys("教资");
          driver.findElement(By.cssSelector("#su")).click();
          WebDriverWait foo = new WebDriverWait(driver,Duration.ofSeconds(3));
          foo.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#\\33  > div > div:nth-child(1) > h3 > a")));
          foo.until(ExpectedConditions.textToBe(By.cssSelector("#\\33  > div > div:nth-child(1) > h3 > a"),"首页-中国教师资格网"));

    }
    public void end_test() throws InterruptedException {
        //释放掉驱动对象，关闭浏览器
        driver.quit();
    }
}
